package com.woniu.adminservice.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.woniu.adminservice.realm.CustomRealm;
import org.apache.commons.collections.map.LinkedMap;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public CustomRealm customRealm(HashedCredentialsMatcher matcher, MemoryConstrainedCacheManager manager){
        CustomRealm realm = new CustomRealm();
        realm.setCacheManager(manager);
        realm.setCredentialsMatcher(matcher);
        return  realm;
    }
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(CustomRealm customRealm, CookieRememberMeManager rememberMeManager){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(customRealm);
        manager.setRememberMeManager(rememberMeManager);
        return manager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(manager);
        shiroFilterFactoryBean.setUnauthorizedUrl("/error/404");
        shiroFilterFactoryBean.setLoginUrl("/login");
        LinkedMap linkedMap = new LinkedMap();
        linkedMap.put("/login","anon");//访问登录
        linkedMap.put("/account/subLogin","anon");
        linkedMap.put("/src/**","anon");
//        linkedMap.put("/roles/list","perms[sys:role:list]");
        linkedMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(linkedMap);
        return shiroFilterFactoryBean;
    }
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1024);
        return matcher;
    }
    @Bean
    public MemoryConstrainedCacheManager manager(){
        return  new MemoryConstrainedCacheManager();
    }
    @Bean
    public CookieRememberMeManager cookieRememberMeManager(SimpleCookie simpleCookie){
        CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
        rememberMeManager.setCookie(simpleCookie);
        return rememberMeManager;}
    @Bean
    public SimpleCookie simpleCookie(){
        SimpleCookie cookie = new SimpleCookie();
        cookie.setName("rememberMe");
        cookie.setMaxAge(1800);
        return cookie;
    }
    @Bean  //thymeleaf整合shiro；支持 shiro标签
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
    //解决shiro 整合 spring aop 的时候，访问资源404的问题
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setUsePrefix(true);
        return defaultAdvisorAutoProxyCreator;
    }


}
